package cn.iocoder.yudao.module.jky.service.goodscatdept;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jky.controller.admin.goodscat.vo.*;
import cn.iocoder.yudao.module.jky.convert.goodscat.GoodsCatConvert;
import cn.iocoder.yudao.module.jky.dal.dataobject.goodscat.GoodsCatDO;
import cn.iocoder.yudao.module.jky.dal.dataobject.goodscatdept.GoodsCatDeptDO;
import cn.iocoder.yudao.module.jky.dal.mysql.goodscat.GoodsCatMapper;
import cn.iocoder.yudao.module.jky.dal.mysql.goodscatdept.GoodsCatDeptMapper;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.jky.enums.ErrorCodeConstants.GOODS_CAT_NOT_EXISTS;

/**
 * 商品分类 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
@DS("master")
public class GoodsCatDeptServiceImpl implements GoodsCatDeptService {

    @Resource
    private GoodsCatDeptMapper goodsCatDeptMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Resource
    private DeptApi deptApi;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void deleteByCatId(Short catId) {
        goodsCatDeptMapper.delete(new LambdaQueryWrapperX<GoodsCatDeptDO>().eq(GoodsCatDeptDO::getCatId, catId));
    }

    @Override
    public String getDeptNameByCatId(Short catId) {
        GoodsCatDeptDO goodsCatDeptDO = goodsCatDeptMapper.selectOne(new LambdaQueryWrapperX<GoodsCatDeptDO>().eq(GoodsCatDeptDO::getCatId, catId));
        CommonResult<DeptRespDTO> dept = deptApi.getDept(goodsCatDeptDO.getDeptId());
        return dept.getData().getName();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void createGoodsCatDept(Short catId,Long deptId) {

        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        assert loginUser != null;
        CommonResult<AdminUserRespDTO> user = adminUserApi.getUser(loginUser.getId());

        if (Objects.nonNull(deptId)) {
            goodsCatDeptMapper.insert(new GoodsCatDeptDO()
                    .setCatId(catId)
                    .setDeptId(deptId));
            return;
        }
        CommonResult<DeptRespDTO> dept = deptApi.getDept(user.getData().getDeptId());
        goodsCatDeptMapper.insert(new GoodsCatDeptDO()
                .setCatId(catId)
                .setDeptId(dept.getData().getId()));

    }

    @Override
    public List<GoodsCatDeptDO> getGoodsCatDeptList() {
        return goodsCatDeptMapper.selectList();
    }

    @Override
    public List<GoodsCatDeptDO> getGoodsCatDeptListByUser(CommonResult<AdminUserRespDTO> user) {
        return goodsCatDeptMapper.selectList(new LambdaQueryWrapperX<GoodsCatDeptDO>()
                .eq(GoodsCatDeptDO::getDeptId, user.getData().getDeptId()));
    }


}
